// Copyright 2006 Mloofle Inc. All Rights Reserved.
// Developed via Cleanroom

/**
 * Represents a retreived set of possible matches in response to a query.
 */
public class ResultSet {
  /*
   * INVARIANTS: 0 <= iterator <= results.length; iterator == 0 implies
   * beginning of result set; iterator == results.length implies end of result
   * set (no more matches)
   */

  public ResultSet(PossibleMatch[] results) {
    // PRE: results is non-null
    this.results = results;
    this.iterator = results.length;
    // POST: object is initialized
    // POST: results is initialized to an array of possible matches
    // POST: iterator is initialized to the END of the list (so that NEXT fails)
  }

  public void begin() {
    iterator = 0;
    // POST: iterator points to beginning of list
  }

  public boolean isEnd() {
    return iterator == results.length;
  }

  public PossibleMatch iterate() {
    // PRE: !isEnd()
    return results[iterator++];
    // POST: next result is returned
  }

  public int size() {
    return results.length;
  }

  public String toString() {
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < results.length; ++i) {
      buf.append(results[i].toString());
      buf.append('\n');
    }
    return buf.toString();
  }
  
  private int iterator;
  private final PossibleMatch[] results;

}
